package cz.idsv.web.mvc.controller;

import java.util.Collection;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import cz.idsv.web.mvc.model.entity.AddressEntity;
import cz.idsv.web.mvc.model.entity.PersonEntity;
import cz.idsv.web.mvc.model.service.AddressService;
import cz.idsv.web.mvc.model.service.PersonService;

/**
 * Person controller.
 * 
 * @author Jan Borzensky
 */
@Controller
@SessionAttributes(value = { "personEntity" })
public class PersonController {

	private static final String LIST_MODEL_KEY = "list";
	private static final String PERSON_ENTITY = "personEntity";
	private static final String ADDRESSES = "addresses";

	protected static Logger logger = Logger.getLogger("PersonController");

	@Autowired()
	private PersonService personService;

	@Autowired()
	private AddressService addressService;

	/**
	 * <p>
	 * Searches for all persons and returns them in a <code>Collection</code>.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/person/list'.
	 * </p>
	 */
	@RequestMapping(value = "/person/list", method = RequestMethod.GET)
	public @ModelAttribute(LIST_MODEL_KEY)
	Collection<PersonEntity> getListOfPerson() {
		logger.debug("Received request to show all persons");
		return personService.findPersons();
	}

	/**
	 * <p>
	 * Create new or update old person <code>PersonEntity</code>.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/person/form'.
	 * </p>
	 */
	@RequestMapping(value="/person/form", method=RequestMethod.GET)
    public ModelAndView getPersonForm(@RequestParam(value = "id", required = false) Integer id) {
    	ModelAndView mv = new ModelAndView();
    	
    	Collection<AddressEntity> addressOfPerson = null;
    	
    	
    	// ziskat osobu
        if(id==null){ 
        	mv.addObject(PERSON_ENTITY, new PersonEntity());
        }else{
        	 PersonEntity person = personService.findPersonById(id);
        	 addressOfPerson = person.getAddress();
        	 mv.addObject(PERSON_ENTITY, person);
        }
        
        // ziskat vsetky adresy 
        Collection<AddressEntity> addresses = addressService.findAddress();
        AddressEntity add1 = new AddressEntity();
        add1.setCity("Krompachy");
        add1.setId(1);
        addresses.add(add1);
        AddressEntity add2= new AddressEntity();
        add2.setCity("Praha");
        add2.setId(2);
        addresses.add(add2);
        mv.addObject(ADDRESSES, addresses);
    	
        
        mv.setViewName("person/form");
        
        return mv;
    }

	/**
	 * <p>
	 * Save new or update old person <code>PersonEntity</code>.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP POST and response '/person/saveForm'.
	 * </p>
	 */
	@RequestMapping(value = "/person/saveForm", method = RequestMethod.POST)
	public String savePersonForm(PersonEntity personEntity, Model model) {
		if (personEntity != null)
			personService.save(personEntity);
		return "redirect:/person/list.html";
	}

	/**
	 * <p>
	 * Deletes a person.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP POST and request '/person/delete'.
	 * </p>
	 */
	@RequestMapping(value = "/person/delete", method = RequestMethod.POST)
	public String delete(PersonEntity personEntity) {
		logger.debug("Received request to delete person with id:"
				+ personEntity.getId());
		personService.delete(personEntity);
		return "redirect:/person/list.html";
	}
}
